<template>
  <card ref="Card" class="mb-15">
    <div slot="header">
      <span>网点充值/扣除</span>
    </div>
    <el-form :model="form" :rules="rules" ref="Form" label-width="115px" hide-required-asterisk>
      <row-col>
        <el-form-item label="网点名称:" prop="userId">
          <search-select-lower v-model="form.userId" @get="get"/>
        </el-form-item>
        <el-form-item prop="price" slot="r">
          <el-input-number v-model="form.price" controls-position="right" :precision="2" :min="0"/>
          <el-button type="primary" @click="addMoney" class="ml-10">充值</el-button>
          <el-button type="danger" @click="reduceMoney">扣除</el-button>
        </el-form-item>
      </row-col>
      <row-col>
        <el-form-item slot="r" label="备注:" prop="notes">
          <el-input v-model="form.notes"></el-input>
        </el-form-item>
      </row-col>
    </el-form>
  </card>
</template>

<script>
  import {addMoneyApi, reduceMoneyApi} from '@/api/fund'
  import SearchSelectLower from "@/views/components/SearchSelectLower";
  import {resetForm} from "@/utils/common";

  export default {
    name: "Money",
    components: {SearchSelectLower},
    data() {
      return {
        form: {
          userId: null,
          username: '',
          notes: '',
          price: 0
        },
        rules: {
          userId: {required: true, message: '请选择网点', trigger: 'change'},
          price: {required: true, message: '请输入金额', trigger: 'change'}
        }
      }
    },
    methods: {
      get(obj) {
        if (!obj) {
          resetForm(this)
          return
        }
        this.form.username = obj.username
      },
      addMoney() {
        this.$refs['Form'].validate((valid) => {
          if (!valid) return false;
          let data = {...this.form};
          this.$msgBox('确认充值？').then(() => {
            this.$refs.Card.start();
            addMoneyApi(data)
              .then(() => {
                this.$refs.Card.stop();
                resetForm(this)
              })
              .catch(() => {
                this.$refs.Card.stop();
              })
          })
        });
      },
      reduceMoney() {
        this.$refs['Form'].validate((valid) => {
          if (!valid) return false;
          let data = {...this.form};
          this.$msgBox('确认扣除？').then(() => {
            this.$refs.Card.start();
            reduceMoneyApi(data)
              .then(() => {
                this.$refs.Card.stop();
                resetForm(this)
              })
              .catch(() => {
                this.$refs.Card.stop();
              })
          })
        });
      }
    }
  }
</script>
